﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Services : SecPage
{
    public string messagetype = "";
    public string message = "";
    ProxyWrapper.UserProxy.UserDetails objUser = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
        if (Session["UserObject"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            if (Request.QueryString["mode"] != null && Request.QueryString["mode"].ToString().Equals("delete"))
            {
                ProxyWrapper.ServicesServiceWrapper _wrap = new ProxyWrapper.ServicesServiceWrapper();
                int serviceId = Convert.ToInt32(Request.QueryString["serviceid"].ToString());
                _wrap.DeleteService(serviceId, objUser.HashCode);
                messagetype = "S";
                message = "Button Deleted Successfully";
            }
            if (Request.QueryString != null && Request.QueryString["msg"] != null)
            {
                messagetype = "S";
                message = Request.QueryString["msg"].ToString();
            }
            string sortOrder = Enums.ServiceSorting.time.ToString();
            if (Request.QueryString != null && Request.QueryString["order"] != null)
            {
                sortOrder = Request.QueryString["order"].ToString();
            }
            DrawPage((Enums.ServiceSorting)Enum.Parse(typeof(Enums.ServiceSorting), sortOrder));
        }
    }

    private void DrawPage(Enums.ServiceSorting sortorder)
    {
        //ProxyWrapper.UserProxy.UserDetails objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["userObject"]);
        ProxyWrapper.ServicesServiceWrapper _srv = new ProxyWrapper.ServicesServiceWrapper();
        List<ProxyWrapper.CommonProxyService.ServiceDetails> objServiceList = _srv.GetServiceDetailsList(objUser.ClientId, objUser.HashCode);
        ProxyWrapper.CommonProxyService.ServiceDetails sd = new ProxyWrapper.CommonProxyService.ServiceDetails();
        //sd.UserNamesList
        switch (sortorder)
        {
            case Enums.ServiceSorting.time:
                objServiceList = objServiceList.OrderByDescending(m => m.ServiceId).ToList<ProxyWrapper.CommonProxyService.ServiceDetails>();
                dateSorter.Style.Add("font-weight", "bolder");
                dateSorter.Style.Add("color", "#000");
                break;
            case Enums.ServiceSorting.alpha:
                objServiceList = objServiceList.OrderBy(m => m.ServiceName).ToList<ProxyWrapper.CommonProxyService.ServiceDetails>();
                nameSorter.Style.Add("font-weight", "bolder");
                nameSorter.Style.Add("color", "#000");
                break;
            case Enums.ServiceSorting.lastscan:
                objServiceList = objServiceList.OrderBy(m => m.ServiceName).ToList<ProxyWrapper.CommonProxyService.ServiceDetails>();
                lastscanSorter.Style.Add("font-weight", "bolder");
                lastscanSorter.Style.Add("color", "#000");
                break;
            default:
                break;
        }

        if (objServiceList != null && objServiceList.Count > 0 && objServiceList[0].ServiceId > 0)
        {
            serviceCount.InnerText = "Showing 1 - " + objServiceList.Count.ToString() + " of " + objServiceList.Count.ToString() + " buttons";
            lstServices.DataSource = objServiceList;
            lstServices.DataBind();
        }
        else
        {
            lstServices.DataSource = new object[] { };
            lstServices.DataBind();
        }
    }

    internal string GetDBDetail(string serviceId)
    {
        List<ProxyWrapper.CommonProxyService.ServiceDetails> objUserList = ((List<ProxyWrapper.CommonProxyService.ServiceDetails>)lstServices.DataSource);
        int dbId = objUserList.First(m => m.ServiceId.ToString() == serviceId).DBId;
        string dbName = objUserList.First(m => m.ServiceId.ToString() == serviceId).DBName;
        if (dbId > 0)
            return "<dt><img src='Images/database16.png' alt='Database' class='attr' /></dt><dd><a href='#?DBId=" + dbId.ToString() + "'>" + dbName + "</a></dd>";
        else
            return "";
    }
    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (messagetype == "E")
            return string.Format(warning, message);
        else if (messagetype == "S")
            return string.Format(result, message);
        else
            return "";

    }
}